package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_ROB_REPORT, desc = "战报")
public class CrossBeautyEscortReportCmd extends CrossCommand {

    int type;
    UserBaseInfo userBaseInfo;
    long dartId;
    long playerId;
    List<Long> memberList = new ArrayList<>();
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortReportReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortReportReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        if(reqMsg.hasUserInfo()) {
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
        }
        if(reqMsg.hasDartId()) {
            dartId = reqMsg.getDartId();
        }
        playerId = packet.getPlayerId();
        memberList.addAll(reqMsg.getMemberIdListList());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(userBaseInfo != null){
            CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        }
        CrossEscortPlayerWrap escortPlayer = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(escortPlayer != null){
            if(userBaseInfo != null) {
                escortPlayer.setUserBaseInfo(userBaseInfo);
            }
            CrossBeautyEscortMgr.getRobbedReport(serverId, packet.getPlayerId(), type, dartId, memberList);
        }else {
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
        }
    }

    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortReportRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortReportRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setType(type);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_ROB_REPORT, respMsg));
        }
    }
}
